using System;
using System.Security.Cryptography;

namespace Sunny.UI;

public struct RSASecretKey
{
	public string PublicKey { get; set; }

	public string PrivateKey { get; set; }

	public RSASecretKey(string privateKey, string publicKey)
	{
		PrivateKey = privateKey;
		PublicKey = publicKey;
	}

	public override string ToString()
	{
		return $"PrivateKey: {PrivateKey}\r\nPublicKey: {PublicKey}";
	}

	public static RSASecretKey GenerateSecretKey(int keySize = 1024)
	{
		RSASecretKey result = default(RSASecretKey);
		using RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider(keySize);
		result.PrivateKey = rSACryptoServiceProvider.ToXmlString(includePrivateParameters: true);
		result.PublicKey = rSACryptoServiceProvider.ToXmlString(includePrivateParameters: false);
		return result;
	}

	public static void GetKey(out string n, out string e, out string d)
	{
		byte[] inData = new byte[64]
		{
			133, 132, 100, 253, 112, 106, 159, 240, 148, 12,
			62, 44, 116, 52, 5, 201, 85, 179, 133, 50,
			152, 113, 249, 65, 33, 95, 2, 158, 234, 86,
			141, 140, 68, 204, 238, 238, 61, 44, 157, 44,
			18, 65, 30, 241, 197, 50, 195, 170, 49, 74,
			82, 216, 232, 175, 66, 244, 114, 161, 42, 13,
			151, 177, 49, 179
		};
		byte[] inData2 = new byte[64]
		{
			153, 152, 202, 184, 94, 215, 229, 220, 40, 92,
			111, 14, 21, 9, 89, 110, 132, 243, 129, 205,
			222, 66, 220, 147, 194, 122, 98, 172, 108, 175,
			222, 116, 227, 203, 96, 32, 56, 156, 33, 195,
			220, 200, 162, 77, 198, 42, 53, 127, 243, 169,
			232, 29, 123, 44, 120, 250, 184, 2, 85, 128,
			155, 194, 165, 203
		};
		BigInteger bigInteger = new BigInteger(inData);
		BigInteger bigInteger2 = new BigInteger(inData2);
		BigInteger bigInteger3 = (bigInteger - 1) * (bigInteger2 - 1);
		BigInteger bigInteger4 = bigInteger * bigInteger2;
		Random rand = new Random();
		BigInteger bigInteger5 = bigInteger3.genCoPrime(512, rand);
		BigInteger bigInteger6 = bigInteger5.modInverse(bigInteger3);
		n = bigInteger4.ToHexString();
		e = bigInteger5.ToHexString();
		d = bigInteger6.ToHexString();
	}
}
